package com.rds.workflow.controller;

import com.rds.common.core.controller.BaseController;
import com.rds.common.core.domain.AjaxResult;
import com.rds.common.core.page.TableDataInfo;
import com.rds.common.utils.SecurityUtils;
import com.rds.system.domain.Sharing;
import com.rds.workflow.domain.RdaWfTask;
import com.rds.workflow.domain.RdaWorkflowObjectRel;
import com.rds.workflow.service.IRdaWfTaskService;
import com.rds.workflow.service.RdaWorkflowObjectRelService;
import com.rds.workflow.utils.WorkflowStateid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/pms/myWorkflow")
public class RdaWfTaskController extends BaseController{

    /**
     * 流程任务完成状态
     * 以下注释后期可删除
     * 开发人员数据库数据:原10000937(Complete)-->10000934(策略)[10000935(创建),10000936(活动),10000937(完成),10000938(作废)]
     * 测试数据库数据:10000828(Complete)-->10000815(WorkflowNodeTemplete)[10000827(作废)-10000830(创建)]
     */
    //private final  static Long taskStateAppoveid = 10000828L;
    /**
     * 流程任务活动状态
     * 以下注释后期可删除
     * 开发人员数据库数据:原10001013(Active)-->10001010(策略)[10001012(创建),10001013(活动),10001014(完成),10001015(作废)]
     * 测试数据库数据:10000821(Active)-->10000813(myWorkflow)[10000819(作废)-10000822(创建)]
     */
    //private final  static Long taskStateActivityid = 10000821L;

    @Autowired
    private IRdaWfTaskService rdaWfTaskService;

    @Autowired
    private RdaWorkflowObjectRelService rdaWorkflowObjectRelService;

    /**
     * 我的流程完成查询
     */
    @PreAuthorize("@ss.hasPermi('pms:myWorkflow:myWorkflowCompleteList')")
    @GetMapping("/myWorkflowCompleteList")
    public TableDataInfo myWorkflowCompleteList(RdaWfTask rdaWfTask){
        startPage();
        rdaWfTask.setUserId(SecurityUtils.getUserId());
//        rdaWfTask.setStateid(taskStateAppoveid);
        rdaWfTask.setStateid(WorkflowStateid.wfTaskCompleteid);
        rdaWfTask.setToolbarType("1");
        return getDataTable(rdaWfTaskService.selectRdaWfTaskList(rdaWfTask));
    }


    /**
     * 我的流程活动查询
     */
    @PreAuthorize("@ss.hasPermi('pms:myWorkflow:myWorkflowActivityList')")
    @GetMapping("/myWorkflowActivityList")
    public TableDataInfo myWorkflowActivityList(RdaWfTask rdaWfTask){
        startPage();
        rdaWfTask.setUserId(SecurityUtils.getUserId());
//        rdaWfTask.setStateid(taskStateActivityid);
        rdaWfTask.setStateid(WorkflowStateid.wfTaskActiveid);
        rdaWfTask.setToolbarType("1");
        return getDataTable(rdaWfTaskService.selectRdaWfTaskList(rdaWfTask));
    }

    /**
     * 流程转派
     */
    @PreAuthorize("@ss.hasPermi('pms:myWorkflow:assignWorkflow')")
    @PostMapping("/assignWorkflow")
    public AjaxResult assignWorkflow(@RequestBody Sharing sharing){
        String idsStr=sharing.getIds();
        String[] ids =idsStr.split(",");
        String[] workflowids =sharing.getToid().split(",");
        RdaWfTask getUserId=rdaWfTaskService.selectRdaWfTaskByid(Long.valueOf(workflowids[0]));
        RdaWfTask rdaWfTask=new RdaWfTask();
        rdaWfTask.setId(Long.valueOf(workflowids[0]));
        rdaWfTask.setUserId(Long.valueOf(ids[0]));
        rdaWfTask.setTransfer(true);
        rdaWfTask.setOriginalId(getUserId.getUserId());
        int row=rdaWfTaskService.updateRdaWfTaskAll(rdaWfTask);
        return toAjax(row);
    }


    /**
     * 审批记录查询
     */
    @PreAuthorize("@ss.hasPermi('pms:myWorkflow:workflowRecord')")
    @GetMapping("/workflowRecord")
    public TableDataInfo workflowRecord(RdaWfTask rdaWfTask){
        startPage();
        List<RdaWfTask> List=new ArrayList<RdaWfTask>();
         List= rdaWfTaskService.selectRdaWfTaskByworkflowid(rdaWfTask);
        if(List.isEmpty()){
            RdaWorkflowObjectRel rdaWorkflowObjectRel=new RdaWorkflowObjectRel();
            rdaWorkflowObjectRel.setObjectid(rdaWfTask.getWorkflowid());
            List<RdaWorkflowObjectRel> rdaWorkflowObjectRelList=rdaWorkflowObjectRelService.selectRdaWorkflowObjectRelList(rdaWorkflowObjectRel);
            if(!rdaWorkflowObjectRelList.isEmpty()){
                rdaWorkflowObjectRel=rdaWorkflowObjectRelList.get(0);
                rdaWfTask=new RdaWfTask();
                rdaWfTask.setWorkflowid(rdaWorkflowObjectRel.getWorkflowid());
                List=rdaWfTaskService.selectRdaWfTaskByworkflowid(rdaWfTask);
            }
        }
        return getDataTable(List);
    }

    @PreAuthorize("@ss.hasPermi('pms:myWorkflow:objectTaskList')")
    @GetMapping("/objectTaskList")
    public TableDataInfo objectTaskList(Long objectid) {
        return getDataTable(rdaWfTaskService.selectRdaWfTaskByObjectId(objectid, WorkflowStateid.workflowActiveid, WorkflowStateid.wfTaskActiveid,SecurityUtils.getUserId()));
    }
}
